import java.math.BigInteger;
import java.util.Scanner;

public class demo01 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        String[] arr=new String[n];
        for (int i = 0; i < n; i++) {
            arr[i]=sc.next();
            String str=new BigInteger(arr[i],16).toString(8);
            arr[i]=str;
        }
        for (int i = 0; i < n; i++) {
            System.out.println(arr[i]);
        }
    }
}
